<?php

include_once 'config.php';

// ===============================
function participantExists($pid) {
    global $DATABASE_NAME;

    //start session
    if (!isset($_SESSION)) {
        session_start();
    }

    $conn = new MongoClient(); // connect
    //get database
    $db = $conn->selectDB($DATABASE_NAME);

    //search for pid
    $cursor = $db->participants->find(array("pid" => $pid));

    //close connection
    $conn->close();

    //send back the result
    if ($cursor->count() > 0) {
        return true;
    }

    return false;
}

// ===============================
function getRecipeRate($recipeNumber) {
    global $DATABASE_NAME;

    //start session
    if (!isset($_SESSION)) {
        session_start();
    }

    $conn = new MongoClient(); // connect
    //get database
    $db = $conn->selectDB($DATABASE_NAME);

    //create collection
    $collection = "recipes_rate";
    $db->createCollection($collection);
    $cursor = $db->$collection->find(array("pid" => $_SESSION['pid'], "recipe_number" => $recipeNumber));

    //close connection
    $conn->close();

    //send back the result
    if ($cursor->count() > 0) {
        $data = $cursor->getNext();
        return $data['recipe_rate'];
    } else {
        return 0;
    }
}

// ===============================
function updateRecipeRate($recipeNumber, $recipeRate) {
    global $DATABASE_NAME;

    //start session
    if (!isset($_SESSION)) {
        session_start();
    }

    $conn = new MongoClient(); // connect
    //get database
    $db = $conn->selectDB($DATABASE_NAME);

    //create collection
    $collection = "recipes_rate";
    $db->createCollection($collection);
    $newdata = array("pid" => $_SESSION['pid'], "recipe_number" => $recipeNumber, "recipe_rate" => $recipeRate);
    $db->$collection->update(array("pid" => $_SESSION['pid'], "recipe_number" => $recipeNumber), $newdata);

    //close connection
    $conn->close();
}

// ===============================
function saveNewRecipeRate($recipeNumber, $recipeRate) {
    global $DATABASE_NAME;

    //start session
    if (!isset($_SESSION)) {
        session_start();
    }

    $conn = new MongoClient(); // connect
    //get database
    $db = $conn->selectDB($DATABASE_NAME);

    //create collection
    $collection = "recipes_rate";
    $db->createCollection($collection);

    $data = array();
    $data['pid'] = $_SESSION['pid'];
    $data['recipe_number'] = $recipeNumber;
    $data['recipe_rate'] = $recipeRate;

    $db->selectCollection($collection)->insert($data);

    //close connection
    $conn->close();
}

//participants
// ===============================
function savePaticipant() {

    if (isset($_SESSION['pid']) && isset($_SESSION['ip'])) {
        global $DATABASE_NAME;

        //start session
        if (!isset($_SESSION)) {
            session_start();
        }
        // connect
        $conn = new MongoClient();
        //get database
        $db = $conn->selectDB($DATABASE_NAME);

        //create object
        $participant = array(
            "pid" => $_SESSION['pid'],
            "ip" => $_SESSION['ip'],
            'ts' => date("Y-m-d H:i:s")
        );

        //set email (if given)
        if (isset($_SESSION['email'])) {
            $participant['email'] = $_SESSION['email'];
        }

        //create collection
        $db->createCollection("participants");

        //inset object
        $db->participants->insert($participant);
        //close connection
        $conn->close();
    }
}

//Created Recipe 
// ===============================
function saveRecipe($postData) {
    // save the results
    global $DATABASE_NAME;

    //start session
    if (!isset($_SESSION)) {
        session_start();
    }

    $conn = new MongoClient(); // connect
    //get database
    $db = $conn->selectDB($DATABASE_NAME);

    //create collection
    $collection = "recipes";
    $db->createCollection($collection);

    $data = array();
    $data['pid'] = $_SESSION['pid'];
    $data['url'] = $postData['url'];

    $db->selectCollection($collection)->insert($data);

    //close connection
    $conn->close();
}

//Intrinsic Motivation Inventory
// ===============================
function saveIMI($postData) {
    //calculate the IMI score
    $imiScore = calculateIMI($postData);

    // save the results
    global $DATABASE_NAME;

    //start session
    if (!isset($_SESSION)) {
        session_start();
    }

    $conn = new MongoClient(); // connect
    //get database
    $db = $conn->selectDB($DATABASE_NAME);

    //create collection
    $collection = "imiq";
    $db->createCollection($collection);

    $data = array();
    $data['pid'] = $_SESSION['pid'];
    $data['score'] = $imiScore;
    $data['questions'] = $postData;

    $db->selectCollection($collection)->insert($data);

    //close connection
    $conn->close();
}

// ===============================
function getIMI($pid) {
    // save the results
    global $DATABASE_NAME;

    //start session
    if (!isset($_SESSION)) {
        session_start();
    }

    $conn = new MongoClient(); // connect
    //get database
    $db = $conn->selectDB($DATABASE_NAME);

    //create collection
    $collection = "imiq";

    $query = array('pid' => $pid);
    $cursor = $db->$collection->find($query);

    //close connection
    $conn->close();

    //send back the result
    if ($cursor->hasNext()) {
        $data = $cursor->getNext();
    }

    return $data['score'];
}

//Motivatinal factors
// ===============================
function saveMotivation($postData) {
    // save the results
    global $DATABASE_NAME;

    //start session
    if (!isset($_SESSION)) {
        session_start();
    }

    $conn = new MongoClient(); // connect
    //get database
    $db = $conn->selectDB($DATABASE_NAME);

    //create collection
    $collection = "motivational_factors";
    $db->createCollection($collection);

    $data = array();
    $data['pid'] = $_SESSION['pid'];
    $data['questions'] = $postData;

    $db->selectCollection($collection)->insert($data);

    //close connection
    $conn->close();
}

//Motivatinal factors
// ===============================
function getMotivation($pid) {
    // save the results
    global $DATABASE_NAME;

    //start session
    if (!isset($_SESSION)) {
        session_start();
    }

    $conn = new MongoClient(); // connect
    //get database
    $db = $conn->selectDB($DATABASE_NAME);

    //create collection
    $collection = "motivational_factors";

    $query = array('pid' => $pid);
    $cursor = $db->$collection->find($query);

    //close connection
    $conn->close();

    //send back the result
    if ($cursor->hasNext()) {
        $data = $cursor->getNext();
    }

    return $data['questions'];
}

//background
// ===============================
function saveBackground($postData) {
    // save the results
    global $DATABASE_NAME;

    //start session
    if (!isset($_SESSION)) {
        session_start();
    }

    $conn = new MongoClient(); // connect
    //get database
    $db = $conn->selectDB($DATABASE_NAME);

    //create collection
    $collection = "background";
    $db->createCollection($collection);

    $data = array();
    $data['pid'] = $_SESSION['pid'];
    $data['background'] = $postData['background'];

    $db->selectCollection($collection)->insert($data);

    //close connection
    $conn->close();
}

// ===============================
function getBackground($pid) {
    // save the results
    global $DATABASE_NAME;

    //start session
    if (!isset($_SESSION)) {
        session_start();
    }

    $conn = new MongoClient(); // connect
    //get database
    $db = $conn->selectDB($DATABASE_NAME);

    //create collection
    $collection = "background";

    $query = array('pid' => $pid);
    $cursor = $db->$collection->find($query);

    //close connection
    $conn->close();

    //send back the result
    if ($cursor->hasNext()) {
        $data = $cursor->getNext();
    }

    return $data["background"];
}

//background assessment questionnaire
// ===============================
function savePT($postData) {
    //calculate personality test results
    $ptResults = calculatePT($postData);

    // save the results
    global $DATABASE_NAME;

    //start session
    if (!isset($_SESSION)) {
        session_start();
    }

    $conn = new MongoClient(); // connect
    //get database
    $db = $conn->selectDB($DATABASE_NAME);

    //create collection
    $collection = "personality_traits";
    $db->createCollection($collection);

    $data = array();
    $data['pid'] = $_SESSION['pid'];
    $data['extraversion'] = $ptResults["extraversion"];
    $data['agreeableness'] = $ptResults["agreeableness"];
    $data['conscientiousness'] = $ptResults["conscientiousness"];
    $data['neuroticism'] = $ptResults["neuroticism"];
    $data['openness'] = $ptResults["openness"];
    $data['questions'] = $postData;

    $db->selectCollection($collection)->insert($data);

    //close connection
    $conn->close();
}

//background assessment questionnaire
// ===============================
function getPT($pid) {
    // save the results
    global $DATABASE_NAME;

    $conn = new MongoClient(); // connect
    //get database
    $db = $conn->selectDB($DATABASE_NAME);
    $data = array();

    //create collection
    $collection = "personality_traits";

    $query = array('pid' => $pid);
    $cursor = $db->$collection->find($query);

    //close connection
    $conn->close();

    //send back the result
    if ($cursor->hasNext()) {
        $data = $cursor->getNext();
    }

    return $data;
}

// ===============================
function getRecipeURL($pid) {
    global $DATABASE_NAME;

    $conn = new MongoClient(); // connect
    //get database
    $db = $conn->selectDB($DATABASE_NAME);

    $query = array('pid' => $pid);
    $cursor = $db->recipes->find($query);

    //close connection
    $conn->close();

    //send back the result
    if ($cursor->hasNext()) {
        $data = $cursor->getNext();
        $url = $data['url'];
    }

    return $url;
}
